home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 037a / tc256d.zip / 3DFRAC.C next >
C/C++ Source or Header  |  1991-02-19  |  7KB  |  274 lines

  1.  
  2.  
  3.  
  4.  
  5. /*
  6.   3dfrac 3.0 by Aaron Contorer 1987-1989
  7.   Draws three-dimensional fractal landscapes.
  8.  
  9.   Please send the author a copy of anything interesting you make
  10.   that uses parts of this code or parts of its design.
  11. */
  12.  
  13.  
  14.  
  15. /* 900821 RAT  Ported to Turbo C's BGI and added Super VGA 256 color support */
  16.  
  17.  
  18. #include <graphics.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <conio.h>
  22. #include <dos.h>
  23. #include "isvga256.h"
  24. #include "isvgadet.h"
  25. #include "VGAEXTRA.H"
  26.  
  27. /*Graphic design*/
  28.  
  29. #define  DEEP 8
  30. #define  XA 0
  31. #define  XB 450
  32. #define  YA 0
  33. #define  ZA 0
  34. #define  YADD 38
  35.  
  36.  
  37. /* Color indices
  38. #define WATERCOLOR 1
  39. #define LANDCOLOR 2 */
  40.  
  41. int WATERCOLOR, LANDCOLOR;
  42.  
  43. int g_driver,g_mode,g_error;   /* !!! */
  44.  
  45.  
  46. float steep;
  47. int sealevel;
  48. int ybottom;
  49.  
  50. /* new vars for 256 color */
  51. DACarray Palette_Array;              /* create array to hold DAC values */
  52. RGB   ColorValue;
  53. float hue,sat,val;
  54. int   count;
  55.  
  56. /* prototypes */
  57.  
  58. float frandom(void);
  59. void waitabit(void);
  60. int tt;
  61.  
  62. /* Func to draw pixels on screen is defined as a macro for speed. */
  63.  
  64. #define addline(x0,y0,z0, x1,y1,z1)\
  65.         if (z1 == -9999) {\
  66.            putpixel((y0 >> 1) + x0, YADD + y0 + z0, WATERCOLOR);\
  67.         } else {\
  68.            tt = (abs(z1) >> 1) + 172;\
  69.            if(tt > 255)\
  70.              tt = 255;\
  71.            setcolor(tt);\
  72.              line((y0 >> 1)+x0, YADD+y0+z0,(y1 >> 1)+x1, YADD+y1+z1);\
  73.           }
  74.  
  75.  
  76. /*----------------- RGB colors from HSV model ---------------------------*/
  77. /* hue    =  pure color of the light
  78.    sat    =  how much white light is mixed in
  79.    value  =  max component of RGB */
  80.  
  81. void hsv2rgb(float h,float s,float v,RGB *Color) {
  82.  
  83. float h1,f,a[7];
  84. int   i;
  85.  
  86.  h1 = h / 60;
  87.  i  = h1;
  88.  f  = h1 - i;
  89.  a[1] = v;
  90.  a[2] = v;
  91.  a[3] = v * (1 - (s*f));
  92.  a[4] = v * (1 - s);
  93.  a[5] = a[4];
  94.  a[6] = v * (1-(s*(1-f)));
  95.  if (i>4) i = i - 4; else i = i + 2;
  96.  Color->Red = a[i];
  97.  if (i>4) i = i - 4; else i = i + 2;
  98.  Color->Green = a[i];
  99.  if (i>4) i = i - 4; else i = i + 2;
  100.  Color->Blue  = a[i];
  101. }
  102.  
  103. float frandom(void) { /* Return random value 0 <= x < 1 */
  104.  
  105.  return rand() / 32768.0;
  106. }
  107.  
  108.  
  109.  
  110. void frac(depth, x0,y0,x2,y2,z0,z1,z2,z3) {
  111. int newz; /* new center point */
  112. int xmid,ymid,z01,z12,z23,z30;
  113.  
  114.   if (kbhit()) return;
  115.  
  116.   if (rand() < 16384) /* 50% chance */
  117.      newz = (z0+z1+z2+z3) / 4 + (int)((rand() / 32768.0) * ((y2-y0)* steep));
  118.   else
  119.      newz = (z0+z1+z2+z3) / 4 - (int)((rand() / 32768.0) * ((y2-y0)* steep));
  120.   xmid = (x0+x2) >> 1;
  121.   ymid = (y0+y2) >> 1;
  122.   z12 = (z1+z2) >> 1;
  123.   z30 = (z3+z0) >> 1;
  124.   z01 = (z0+z1) >> 1;
  125.   z23 = (z2+z3) >> 1;
  126.   depth--;
  127.   if (depth>=0 ) {
  128.     frac(depth, x0,y0, xmid,ymid, z0,z01,newz,z30);
  129.     frac(depth, xmid,y0, x2,ymid, z01,z1,z12,newz);
  130.     frac(depth, x0,ymid, xmid,y2, z30,newz,z23,z3);
  131.     frac(depth, xmid,ymid, x2,y2, newz,z12,z2,z23);
  132.   } else {
  133.     if (newz<=sealevel ) { /*above sea level*/
  134.       /*L to R*/
  135.       addline(xmid,ymid,newz, x2,ymid,z12);
  136.       addline(xmid,ymid,newz, x0,ymid,z30);
  137.     } else {
  138.       /*below "sea level"*/
  139.       addline(xmid,ymid,sealevel, 0,0,-9999);
  140.     }
  141.   }
  142. }
  143.  
  144.  
  145. void waitabit(void) {
  146.    getch();
  147. }
  148.  
  149.  
  150. int main()
  151. {
  152. char c;
  153. int i;
  154. unsigned char gval;
  155. unsigned seed;
  156.  
  157.   clrscr();
  158.   printf("\n3dfrac version 3.0 by Aaron Contorer, 1987-1989\n");
  159.   printf("This is a graphics demo program that creates images resembling\n");
  160.   printf("landscapes using three-dimensional fractal geometry.  These\n");
  161.   printf("images are generated as you watch, using random numbers and a\n");
  162.   printf("simple mathematical formula.\n\n");
  163.   printf("This program works only on machines with EGA or VGA graphics\n");
  164.   printf("cards.  You are free to copy it if you wish.\n");
  165.   printf("Press <space> to run it now, or <esc> to cancel.\n");
  166.   printf("Once the program is running, presssing any key will stop it.\n\n");
  167.   printf("Author:  Aaron Contorer, president, Contorer Computing\n");
  168.   printf("Mailing address:  Post Office Box 5056, Champaign, IL 61825, USA\n");
  169.  
  170.   printf("\n\n\nConverted to Turbo C and added Super VGA 256 color support\n");
  171.   printf("              - Reagan Thomas of Thomas Design\n");
  172.  
  173.   do {
  174.     c=getch();
  175.   } while (c != 27 && c != ' ');
  176.   if (c==32 ) {
  177.  
  178.      installuserdriver("ISVGA256",DetectISVGA256);
  179.      g_driver = DETECT;
  180.      initgraph(&g_driver, &g_mode,"");
  181.  
  182.       g_error = graphresult();
  183.      if (g_error != 0) {
  184.         printf("%s \n",grapherrormsg(g_error));
  185.         exit(1);
  186.      }
  187.  
  188.      ybottom = getmaxy()-38;
  189.  
  190.      WATERCOLOR = 46;
  191.      LANDCOLOR = GREEN;
  192.  
  193.       hue = 0;
  194.       sat = 1.0;
  195.       val = 63.0;
  196.       for (i=1;  i<100;  i++) {
  197.          hsv2rgb(hue,sat,val,&ColorValue);
  198.         Palette_Array[i][red] = ColorValue.Red;
  199.         Palette_Array[i][grn] = ColorValue.Green;
  200.         Palette_Array[i][blu] = ColorValue.Blue;
  201.         hue = hue + 3.0;
  202.       }
  203.       hue = 0;
  204.       sat = 1.0;
  205.       val = 43.0;
  206.       for (i=100;  i<177;  i++) {       /* blueish-green */
  207.          hsv2rgb(hue,sat,val,&ColorValue);
  208.         Palette_Array[i][red] = ColorValue.Red;
  209.         Palette_Array[i][grn] = ColorValue.Green;
  210.         Palette_Array[i][blu] = ColorValue.Blue;
  211.          hue = hue + 2.80;
  212.       }
  213.       for (i=177;  i<183;  i++) {       /* green */
  214.          hsv2rgb(hue,sat,val,&ColorValue);
  215.         Palette_Array[i][red] = ColorValue.Red;
  216.         Palette_Array[i][grn] = ColorValue.Green;
  217.         Palette_Array[i][blu] = ColorValue.Blue;
  218.         hue = hue + 14;
  219.       }
  220.       hue = 299.6;                      /* light brown */
  221.       for (i=183;  i<201;  i++) {
  222.          hsv2rgb(hue,sat,val,&ColorValue);
  223.         Palette_Array[i][red] = ColorValue.Red;
  224.         Palette_Array[i][grn] = ColorValue.Green;
  225.         Palette_Array[i][blu] = ColorValue.Blue;
  226.         hue = hue + 1.4;
  227.     }
  228.     /*val = 38.0; */
  229.     for (i=201;  i<222;  i++) {         /* reddish-brown */
  230.         hsv2rgb(hue,sat,val,&ColorValue);
  231.        Palette_Array[i][red] = ColorValue.Red;
  232.        Palette_Array[i][grn] = ColorValue.Green;
  233.        Palette_Array[i][blu] = ColorValue.Blue;
  234.        hue = hue + 0.6;
  235.         /*sat = sat - 0.03; */
  236.     }
  237.     Palette_Array[0][red]     = 1;     /* Set first DAC register to black */
  238.     Palette_Array[0][grn]     = 2;
  239.     Palette_Array[0][blu]     = 3;
  240.  
  241.     for(i=223, gval=0x20; i < 255; i++, gval++) {
  242.        Palette_Array[i][red]     = gval;     /* Set first DAC register to black */
  243.        Palette_Array[i][grn]     = gval;
  244.        Palette_Array[i][blu]     = gval;
  245.     }
  246.  
  247.     Palette_Array[255][red]   = 0x3f;  /* Set last DAC register to white */
  248.     Palette_Array[255][grn]   = 0x3f;
  249.     Palette_Array[255][blu]   = 0x3f;
  250.     dacpalette(Palette_Array);      /* load DAC registers with new colors */
  251.  
  252.     _AX = 0;
  253.     geninterrupt(0x1a);    /* get lo word of timer tick count */
  254.     seed = _DX;
  255.  
  256.     srand(seed);
  257.  
  258.     while (1) {
  259.      cleardevice();
  260.      steep = (frandom() / 2.5) + 0.55;
  261.      sealevel = (int)(17*frandom()) - 8;
  262.      frac(DEEP, XA,YA,XB,ybottom,ZA,ZA,ZA,ZA);
  263.      if (!kbhit())
  264.         waitabit();
  265.      else {
  266.         if(getch() == 0x1b)
  267.           break;
  268.      }
  269.     }
  270.     restorecrtmode();
  271.   }
  272.   return(0);
  273. }
  274.